<template>
  <div class="mod-menu">
    <div class="">
      <el-form :inline="true" :model="pamars">
        <el-form-item label="活动名称">
          <el-input v-model="pamars.name" placeholder="活动名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="dataList" row-key="Id" border style="width: 100%; ">
      <el-table-column prop="name" align="center" label="活动名称">
      </el-table-column>
      <el-table-column prop="money" align="center" label="充值金额">
      </el-table-column>
      <el-table-column prop="append" align="center" label="赠送金额">
      </el-table-column>
      <el-table-column prop="isShow" align="center" label="是否开启">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.added" :active-value="1" :inactive-value="0" inactive-color="#B9B9B9" @change="changeSwitch(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" class="c-error" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pamars.page" :page-sizes="[10, 20, 50, 100]" :page-size="pamars.limit" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <topUp-add-update v-if="addOrUpdateVisible" ref="topUpAddUpdate" @updataList='updataList'></topUp-add-update>
  </div>
</template>

<script>
import topUpAddUpdate from './topUp-add-update.vue'
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      pamars: {
        page: 1,
        limit: 10,
        name: ''
      },
      totalPage: 0,
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    topUpAddUpdate
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 更新列表
    updataList (e) {
      if (e == 1) {
        this.getList()
      }
    },
    // 获取列表数据
    getList () {
      seviceApi.getRefillCardList(this.pamars).then(res => {
        if (res && res.data.code == 200) {
          this.dataList = res.data.page.list
          this.totalPage = res.data.page.totalCount
        }
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pamars.limit = val
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pamars.page = val
      this.getList()
    },
    // 查询
    getDataList () {
      this.pamars.page = 1
      this.getList()
    },
    // 是否开启
    changeSwitch (e) {
      var pamar = {
        id: e.id,
        added: e.added
      }
      seviceApi.postRefillCardOperation(pamar).then(res => {
        if (res && res.data.code == 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getList()
        }
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.topUpAddUpdate.init(id)
      })
    },
    // 删除
    del (id) {
      let arry = [id]
      this.$confirm(`确定要删除该条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        seviceApi.delRefillCard(arry).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getList()
          }
        })
      }).catch(() => { })
    }
  }
}
</script>

<style lang="scss">
.mt30 {
  margin-top: 30px;
}
</style>
